package com.example.jbmsuppliers.sinopec.activity;


import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.adapter.FragmentAdapter;
import com.example.jbmsuppliers.sinopec.adapter.MyFragmentAdapter_lz;
import com.example.jbmsuppliers.sinopec.fragment.ListFragment;
import com.example.jbmsuppliers.sinopec.net.NetDeceVersion;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.example.permission.myjbm.model.MyFragmentModel;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jbm.jbmsupplier.util.IInformationCtrl;
import com.jbm.jbmsupplier.util.InfomationEntity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by 家帮忙 on 2017/3/2.
 */

public class MyLlaciton extends ActivityUse implements MyFragmentAdapter_lz.InformUpdate {
    private final int TABHOST = 000;
    private final int MYLLACTION = 0001;
    @InjectView(R.id.goback_ps_all)
    Button gobackPsAll;

    @InjectView(R.id.tabs)
    TabLayout tabs;
    @InjectView(R.id.viewpager)
    ViewPager viewpager;
    @InjectView(R.id.pull_refresh_list)
    PullToRefreshListView pullRefreshList;
    @InjectView(R.id.lv_default)
    ImageView lvDefault;
    private int num = 5;
    private NetDeceVersion netDeceVersion;
    private DialogLoading dialaciton;
    private SimpleDateFormat df;
    private MyFragmentModel model;
    private int page;
    private  List<InfomationEntity> arr1;

    @Override
    protected int setContentView1() {
        return R.layout.lz_myallaction;
    }

    @Override
    protected void initView() {
        ExitAppUtlis.getInstance().addActivity(this);
        ButterKnife.inject(this);
        arr1=new  ArrayList<InfomationEntity>();
        df = new SimpleDateFormat("yyyy-MM-dd-HH-mm", Locale.CHINA);
        model=new MyFragmentModel();
        netDeceVersion = new NetDeceVersion(handler);
        netDeceVersion.setMyllaction(0, num, TABHOST);
        dialaciton = new DialogLoading(this, "加载中...", true);
        dialaciton.show();
        pulltoUpRefresh();
    }

    private void pulltoUpRefresh() {
        pullRefreshList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page=0;
                netDeceVersion.setMyllaction(page, num, TABHOST);
                if (dialaciton != null) {
                    dialaciton.show();
                }
                pullRefreshList.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page+=5;

                netDeceVersion.setMyllaction(page, num, TABHOST);
                if (dialaciton != null) {
                    dialaciton.show();
                }
                pullRefreshList.onRefreshComplete();
            }
        });
        pullRefreshList.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                page+=5;
                netDeceVersion.setMyllaction(page, num, TABHOST);

            }
        });
    }

    @OnClick(R.id.goback_ps_all)
    public void onClick() {
        finish();
    }

    private MyFragmentAdapter_lz adapter;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case TABHOST: {
                    IInformationCtrl infomation = (IInformationCtrl) msg.obj;
                    if (infomation.back) {
                        if (dialaciton != null) {
                            dialaciton.dismiss();
                        }

                        for (int i=0;i<infomation.infomationList.size();i++){
                            model.modifyDate(infomation.infomationList.get(i),df);
                        }

                        if (page == 0) {
                            arr1.clear();
                            arr1.addAll(infomation.infomationList);
                            adapter = new MyFragmentAdapter_lz(arr1, MyLlaciton.this, "搜藏", null,df);
                            pullRefreshList.setAdapter(adapter);
                        } else {
                            arr1.addAll(infomation.infomationList);
                            adapter.setNotify(arr1);
                        }
                        if (arr1.size()==0){
                            lvDefault.setVisibility(View.VISIBLE);
                        }else{
                            lvDefault.setVisibility(View.GONE);
                        }


                    } else if (infomation.equals("time out")) {
                        hint(infomation.errorinfo);
                    } else {
                        hint(infomation.errorinfo);
                    }
                    break;
                }
            }


            super.handleMessage(msg);
        }
    };

    public void setTitle() {//设置标题栏
        List<String> titles = new ArrayList<>();
        titles.add("商品");
        titles.add("求助");
        titles.add("互助");
        titles.add("其他");
        for (int i = 0; i < titles.size(); i++) {
            tabs.addTab(tabs.newTab().setText(titles.get(i)));
        }
        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < titles.size(); i++) {
            fragments.add(new ListFragment());
        }
        FragmentAdapter mFragmentAdapteradapter = new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
        //给ViewPager设置适配器
        viewpager.setAdapter(mFragmentAdapteradapter);
        //将TabLayout和ViewPager关联起来。
        tabs.setupWithViewPager(viewpager);
        //给TabLayout设置适配器
        tabs.setTabsFromPagerAdapter(mFragmentAdapteradapter);
    }


    @Override
    public void MyLissueUpdate(int i) {

    }

    @Override
    public void MyLlactionUpdate() {
        netDeceVersion.setMyllaction(0, num, TABHOST);


    }
}
